﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
namespace syc.Sale
{
    public partial class 原料采购计划录入 : System.Web.UI.Page
    {
        protected double 油管数量;
        protected double 油管单价;

        protected double 套管数量;
        protected double 套管单价;

        protected double 油管N80数量;
        protected double 油管N80单价;

        protected double 套管N80数量;
        protected double 套管N80单价;

        protected double 油管接箍数量;
        protected double 油管接箍单价;

        protected double 油管N80接箍数量;
        protected double 油管N80接箍单价;

        protected double 套管接箍数量;
        protected double 套管接箍单价;

        protected double 套管N80接箍数量;
        protected double 套管N80接箍单价;

        protected double 成品套管数量;
        protected double 成品套管单价;

        protected double 成品套管N80数量;
        protected double 成品套管N80单价;


        protected double 油管光管;
        protected double 套管光管;
        protected double 套管N80光管;
        protected double 油管接箍光管;
        protected double 套管接箍光管;
        protected double 成品套管接箍;
        double[] chanliang = new double[20];
        protected HSSFWorkbook hssfworkbook;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            string y = Request.QueryString["y"];
            string m = Request.QueryString["m"];
          
            if (m == "0")
            {
                this.JiDu0.Visible = false;
                this.YueFen.Visible = false;
                this.Select.SelectedValue = "年度";
                this.Select.Enabled = false;
            }
            else
            {
                if (Request.QueryString["y"] != null)
                {
                    this.NianDu.Text = y;
                    this.NianDu.Enabled = false;
                    this.Select.Enabled = false;
                }
                else
                {
                    this.Select.Enabled = true;
                }
                if (Request.QueryString["m"] != null)
                {
                    this.YueFen.Text = m;
                    this.YueFen.Enabled = false;
                    this.Select.Enabled = false;
                }
                else
                {
                    this.Select.Enabled = true;
                }
                this.JiDu0.Visible = true;
                this.YueFen.Visible = true;             
            }
            if (!this.IsPostBack)
            {
                KuCun();
                Select_SelectedIndexChanged(null, null);
                this.NianDu.Text = DateTime.Today.Year.ToString();
                this.YueFen.Text = DateTime.Today.Month.ToString();
                

                if (y == null || m == null || this.NianDu.Items.FindByValue(y) == null || this.YueFen.Items.FindByValue(m) == null)
                {

                }
                else
                {
                    int year = int.Parse(y);
                    int month = int.Parse(m);
                    this.NianDu.Text = y;
                    this.YueFen.Text = m;

                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;

                    Cmd.CommandText = "select * from 原料采购计划 where 年度=" + y + " and 季度=" + m;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.YouGuan.Text = ((double)Reader["油管"]).ToString();
                        this.TaoGuan.Text = ((double)Reader["套管"]).ToString();                       
                        this.YouGuanJieGu.Text = ((double)Reader["油管接箍"]).ToString();
                        this.TaoGuanJieGu.Text = ((double)Reader["套管接箍"]).ToString();
                        this.ChengPinJieGu.Text = ((double)Reader["成品套管接箍"]).ToString();
                        this.YouGuan_N80.Text = ((double)Reader["油管N80"]).ToString();
                        this.TaoGuan_N80.Text = ((double)Reader["套管N80"]).ToString();
                        this.YouGuanJieGu_N80.Text = ((double)Reader["油管N80接箍"]).ToString();
                        this.TaoGuanJieGu_N80.Text = ((double)Reader["套管N80接箍"]).ToString();
                        this.ChengPinJieGu_N80.Text = ((double)Reader["成品套管N80接箍"]).ToString();
                        this.YouGuanJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["油管单价"]);
                        this.TaoGuanJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["套管单价"]);
                        this.YouGuanJieGuJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["油管接箍单价"]);
                        this.TaoGuanJieGuJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["套管接箍单价"]);
                        this.ChengPinJieGuJiHuaZhiJinDanJia.Text = string.Format("{0:0.00}", Reader["成品套管接箍单价"]);
                        this.YouGuanJiHuaZhiJinDanJia_N80.Text = string.Format("{0:0.00}", Reader["油管N80单价"]);
                        this.TaoGuanJiHuaZhiJinDanJia_N80.Text = string.Format("{0:0.00}", Reader["套管N80单价"]);
                        this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text = string.Format("{0:0.00}", Reader["油管N80接箍单价"]);
                        this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text = string.Format("{0:0.00}", Reader["套管N80接箍单价"]);
                        this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text = string.Format("{0:0.00}", Reader["成品套管N80接箍单价"]);

                        this.YouGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text) * (double)Reader["油管"]));
                        this.TaoGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double)Reader["套管"]));
                        this.YouGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text) * (double)Reader["油管接箍"]));
                        this.TaoGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text) * (double)Reader["套管接箍"]));
                        this.ChengPinJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text) * (double)Reader["成品套管接箍"]));

                        this.YouGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia_N80.Text) * (double)Reader["油管N80"]));
                        this.TaoGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text) * (double)Reader["套管N80"]));
                        this.YouGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double)Reader["油管N80接箍"]));
                        this.TaoGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double)Reader["套管N80接箍"]));
                        this.ChengPinJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text) * (double)Reader["成品套管N80接箍"]));
                        this.NianDu.Enabled = false;
                        this.YueFen.Enabled = false;
                    }
                    Reader.Close();

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                    Conn.Close();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
             
              
            }
        }
        //原料库存
        private void KuCun()
        {  

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string sql = @"
            declare @油管光管收入 float
            declare @套管光管收入  float
            declare @套管N80光管收入  float
            declare @油管接箍光管收入  float
            declare @套管接箍光管收入    float      
            declare @成品套管接箍收入  float   
            
            declare @油管光管发出 float
            declare @套管光管发出 float
            declare @套管N80光管发出 float
            declare @油管接箍光管发出 float
            declare @套管接箍光管发出   float      
            declare @成品套管接箍发出 float   

            declare @油管光管 float
            declare @套管光管 float
            declare @套管N80光管 float
            declare @油管接箍光管 float
            declare @套管接箍光管   float      
            declare @成品套管接箍 float      
           
            select @油管光管收入=isnull(SUM(总重量),0) from 原料到发记录 where 原料ID=1 and 到货日期<='@日期' 
            select @油管光管发出=isnull(SUM(重量),0)   from 原料发货记录 where 原料ID=1 and 日期<='@日期' 
            select @套管光管收入=isnull(SUM(总重量),0) from 原料到发记录 where 原料ID=2 and 到货日期<='@日期' 
            select @套管光管发出=isnull(SUM(重量),0)   from 原料发货记录 where 原料ID=2 and 日期<='@日期' 
            select @套管N80光管收入=isnull(SUM(总重量),0) from 原料到发记录 where 原料ID=102 and 到货日期<='@日期' 
            select @套管N80光管发出=isnull(SUM(重量),0)  from 原料发货记录 where 原料ID=102 and 日期<='@日期' 
            select @油管接箍光管收入=isnull(SUM(总重量),0) from 原料到发记录 where 原料ID=3 and 到货日期<='@日期'
            select @油管接箍光管发出=isnull(SUM(重量),0)   from 原料发货记录 where 原料ID=3 and 日期<='@日期'
            select @套管接箍光管收入=isnull(SUM(总重量),0) from 原料到发记录 where 原料ID=4 and 到货日期<='@日期'
            select @套管接箍光管发出=isnull(SUM(重量),0)  from 原料发货记录 where 原料ID=4 and 日期<='@日期'  
            select @成品套管接箍收入=isnull(SUM(总支数),0) from 原料到发记录 where 原料ID=6 and 到货日期<='@日期'
            select @成品套管接箍发出=isnull(SUM(支数),0)   from 原料发货记录 where 原料ID=6 and 日期<='@日期'  
           
             set @油管光管=@油管光管收入-@油管光管发出
             set @套管光管=@套管光管收入-@套管光管发出
             set @套管N80光管=@套管N80光管收入-@套管N80光管发出
             set @油管接箍光管=@油管接箍光管收入-@油管接箍光管发出
             set @套管接箍光管=@套管接箍光管收入-@套管接箍光管发出
             set @成品套管接箍=@成品套管接箍收入-@成品套管接箍发出

            select @油管光管,@套管光管,@套管N80光管,@油管接箍光管,@套管接箍光管,@成品套管接箍
            ";
            sql = sql.Replace("@日期", DateTime.Today.ToString());
            Cmd.CommandText = sql;
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                油管光管 = Reader.GetDouble(0);
                套管光管 = Reader.GetDouble(1);
                套管N80光管 = Reader.GetDouble(2);
                油管接箍光管 = Reader.GetDouble(3);
                套管接箍光管 = Reader.GetDouble(4);
                成品套管接箍 = Reader.GetDouble(5);
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
            this.Label_Result.Text = "现有库存数  油管光管：" + 油管光管 + " 吨" + " 套管光管：" + 套管光管 + " 吨" + " 套管N80光管：" + 套管N80光管 + " 吨" + " 油管接箍光管：" + 油管接箍光管 + " 吨" + " 套管接箍光管：" + 套管接箍光管 + " 吨" + " 成品套管接箍：" + 成品套管接箍 + " 支";
        }
        private bool CheckInput()
        {
            //----------录入量
            if (!double.TryParse(this.YouGuan.Text, out chanliang[0]))
            {
                this.Message.Text = "·原料油管 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[0] < 0)
            {
                this.Message.Text = "·原料油管 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuan_N80.Text, out chanliang[1]))
            {
                this.Message.Text = "·原料油管(N80) 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[1] < 0)
            {
                this.Message.Text = "·原料油管(N80) 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuan.Text, out chanliang[2]))
            {
                this.Message.Text = "·原料套管 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[2] < 0)
            {
                this.Message.Text = "·原料套管 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuan_N80.Text, out chanliang[3]))
            {
                this.Message.Text = "·原料套管(N80) 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[3] < 0)
            {
                this.Message.Text = "·原料套管(N80) 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuanJieGu.Text, out chanliang[4]))
            {
                this.Message.Text = "·原料油管接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[4] < 0)
            {
                this.Message.Text = "·原料油管接箍 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuanJieGu_N80.Text, out chanliang[5]))
            {
                this.Message.Text = "·原料油管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[5] < 0)
            {
                this.Message.Text = "·原料油管(N80)接箍 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJieGu.Text, out chanliang[6]))
            {
                this.Message.Text = "·原料套管接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[6] < 0)
            {
                this.Message.Text = "·原料套管接箍 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJieGu_N80.Text, out chanliang[7]))
            {
                this.Message.Text = "·原料套管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[7] < 0)
            {
                this.Message.Text = "·原料套管(N80)接箍 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.ChengPinJieGu.Text, out chanliang[8]))
            {
                this.Message.Text = "·原料成品套管接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[8] < 0)
            {
                this.Message.Text = "·原料成品套管接箍 计划购进量不能小于0";
                return false;
            }
            if (!double.TryParse(this.ChengPinJieGu_N80.Text, out chanliang[9]))
            {
                this.Message.Text = "·原料成品套管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else if (chanliang[9] < 0)
            {
                this.Message.Text = "·原料成品套管(N80)接箍 计划购进量不能小于0";
                return false;
            }
            //----------录入单价
            if (!double.TryParse(this.YouGuanJiHuaZhiJinDanJia.Text, out chanliang[10]))
            {
                this.Message.Text = "·原料油管 油管单价录入不正确";
                return false;
            }
            else if (chanliang[10] < 0)
            {
                this.Message.Text = "·原料油管 油管单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuanJiHuaZhiJinDanJia_N80.Text, out chanliang[11]))
            {
                this.Message.Text = "·原料油管(N80) 油管单价录入不正确";
                return false;
            }
            else if (chanliang[11] < 0)
            {
                this.Message.Text = "·原料油管(N80) 油管单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJiHuaZhiJinDanJia.Text, out chanliang[12]))
            {
                this.Message.Text = "·原料套管 套管单价录入不正确";
                return false;
            }
            else if (chanliang[12] < 0)
            {
                this.Message.Text = "·原料套管 套管单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text, out chanliang[13]))
            {
                this.Message.Text = "·原料套管(N80) 套管单价录入不正确";
                return false;
            }
            else if (chanliang[13] < 0)
            {
                this.Message.Text = "·原料套管(N80) 套管单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text, out chanliang[14]))
            {
                this.Message.Text = "·原料油管接箍 油管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[14] < 0)
            {
                this.Message.Text = "·原料油管接箍 油管接箍单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang[15]))
            {
                this.Message.Text = "·原料油管(N80)接箍 油管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[15] < 0)
            {
                this.Message.Text = "·原料油管(N80)接箍 油管接箍单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text, out chanliang[16]))
            {
                this.Message.Text = "·原料套管接箍 套管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[16] < 0)
            {
                this.Message.Text = "·原料套管接箍 套管接箍单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang[17]))
            {
                this.Message.Text = "·原料套管(N80)接箍 套管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[17] < 0)
            {
                this.Message.Text = "·原料套管(N80)接箍 套管接箍单价不能小于0";
                return false;
            }
            if (!double.TryParse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text, out chanliang[18]))
            {
                this.Message.Text = "·原料成品套管接箍 成品套管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[18] < 0)
            {
               this.Message.Text = "·原料成品套管接箍 成品套管接箍单价不能小于0";
               return false;
            }
            if (!double.TryParse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang[19]))
            {
                this.Message.Text = "·原料成品套管(N80)接箍 成品套管接箍单价录入不正确";
                return false;
            }
            else if (chanliang[19] < 0)
            {
                this.Message.Text = "·原料成品套管(N80)接箍 成品套管接箍单价不能小于0";
                return false;
            }
            return true;
        }
        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("模板/原料采购计划.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "陕西延长石油材料有限公司";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "原料采购计划";
            hssfworkbook.SummaryInformation = si;
        }
        protected void WriteToFile()
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("原料采购计划") + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            if (!CheckInput())
            {
                return;
            }
            
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                if (this.NianDu.Enabled && this.JiDu0.Enabled)
                {

                    Cmd.CommandText =
    "if EXISTS(select * from 原料采购计划 where 年度=" + this.NianDu.Text + " and 季度=" + this.YueFen.Text + ")\n"
    + "begin\n"
    + "return\n"
    + "end\n"
    + "else\n"
    + "begin\n"
    + "insert into 原料采购计划(年度,季度,油管,油管单价,套管,套管单价,油管接箍,油管接箍单价,套管接箍,套管接箍单价,成品套管接箍,成品套管接箍单价,油管N80,油管N80单价,套管N80,套管N80单价,油管N80接箍,油管N80接箍单价,套管N80接箍,套管N80接箍单价,成品套管N80接箍,成品套管N80接箍单价,日期)"
   + "values(" + this.NianDu.Text + "," + this.YueFen.Text + "," + double.Parse(this.YouGuan.Text) + "," + this.YouGuanJiHuaZhiJinDanJia.Text + "," + double.Parse(this.TaoGuan.Text) + "," + this.TaoGuanJiHuaZhiJinDanJia.Text + "," + double.Parse(this.YouGuanJieGu.Text) + "," + this.YouGuanJieGuJiHuaZhiJinDanJia.Text + "," + double.Parse(this.TaoGuanJieGu.Text) + "," + this.TaoGuanJieGuJiHuaZhiJinDanJia.Text + "," + double.Parse(this.ChengPinJieGu.Text) + "," + this.ChengPinJieGuJiHuaZhiJinDanJia.Text + "," + double.Parse(this.YouGuan_N80.Text) + "," + this.YouGuanJiHuaZhiJinDanJia_N80.Text + "," + double.Parse(this.TaoGuan_N80.Text) + "," + this.TaoGuanJiHuaZhiJinDanJia_N80.Text + "," + double.Parse(this.YouGuanJieGu_N80.Text) + "," + this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text + "," + double.Parse(this.TaoGuanJieGu_N80.Text) + "," + this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text + "," + double.Parse(this.ChengPinJieGu_N80.Text) + "," + this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'"
    + ")\n"  
    + "end";
                }
                else
                {
                    Cmd.CommandText =
                   "update 原料采购计划 set 油管=" + chanliang[0]
                   + ",油管N80=" + chanliang[1]
                   + ",套管=" + chanliang[2]
                   + ",套管N80=" + chanliang[3]
                   + ",油管接箍=" + chanliang[4]
                   + ",油管N80接箍=" + chanliang[5]
                   + ",套管接箍=" + chanliang[6]
                   + ",套管N80接箍=" + chanliang[7]
                   + ",成品套管接箍=" + chanliang[8]
                   + ",成品套管N80接箍=" + chanliang[9]

                   + ",油管单价=" + chanliang[10]
                   + ",油管N80单价=" + chanliang[11]
                   + ",套管单价=" + chanliang[12]
                   + ",套管N80单价=" + chanliang[13]
                   + ",油管接箍单价=" + chanliang[14]
                   + ",油管N80接箍单价=" + chanliang[15]
                   + ",套管接箍单价=" + chanliang[16]
                   + ",套管N80接箍单价=" + chanliang[17]
                   + ",成品套管接箍单价=" + chanliang[18]
                   + ",成品套管N80接箍单价=" + chanliang[19]                

                   + " where 年度="
                   + this.NianDu.Text + " and 季度=" + this.YueFen.Text;
                }


                if (Cmd.ExecuteNonQuery() < 1)
                {
                    if (this.NianDu.Enabled)
                    {
                        this.Message.Text = "·保存失败，计划已经存在";
                    }
                    else
                    {
                        this.Message.Text = "·保存失败，您正在修改的计划可能已经被删除";
                    }
                }
                else
                {
                    this.Message.Text = "·保存成功";
                    this.YouGuan.Enabled = false;
                    this.YouGuan.CssClass = "blank_center";
                    this.YouGuanJiHuaZhiJinDanJia.Enabled = false;
                    this.YouGuanJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.YouGuan_N80.Enabled = false;
                    this.YouGuan_N80.CssClass = "blank_center";
                    this.YouGuanJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.YouGuanJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.TaoGuan.Enabled = false;
                    this.TaoGuan.CssClass = "blank_center";
                    this.TaoGuanJiHuaZhiJinDanJia.Enabled = false;
                    this.TaoGuanJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.TaoGuan_N80.Enabled = false;
                    this.TaoGuan_N80.CssClass = "blank_center";
                    this.TaoGuanJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.TaoGuanJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.YouGuanJieGu.Enabled = false;
                    this.YouGuanJieGu.CssClass = "blank_center";
                    this.YouGuanJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.YouGuanJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.YouGuanJieGu_N80.Enabled = false;
                    this.YouGuanJieGu_N80.CssClass = "blank_center";
                    this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.YouGuanJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.TaoGuanJieGu.Enabled = false;
                    this.TaoGuanJieGu.CssClass = "blank_center";
                    this.TaoGuanJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.TaoGuanJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.TaoGuanJieGu_N80.Enabled = false;
                    this.TaoGuanJieGu_N80.CssClass = "blank_center";
                    this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.ChengPinJieGu.Enabled = false;
                    this.ChengPinJieGu.CssClass = "blank_center";
                    this.ChengPinJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.ChengPinJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.ChengPinJieGu_N80.Enabled = false;
                    this.ChengPinJieGu_N80.CssClass = "blank_center";
                    this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.ChengPinJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";
                    this.NianDu.Enabled = false;
                    this.YueFen.Enabled = false;
                    Button_Calc_Click(null, null);
                }
            }
            catch(Exception ex)
            {
                string s = ex.Message;
                this.Message.Text = "·保存失败，计划已经存在";
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();

        }
      
        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                this.YouGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuan.Text))));
                this.TaoGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuan.Text))));
                this.YouGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuanJieGu.Text))));
                this.TaoGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuanJieGu.Text))));
                this.ChengPinJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.ChengPinJieGu.Text))));
                this.YouGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuan_N80.Text))));
                this.TaoGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuan_N80.Text))));
                this.YouGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuanJieGu_N80.Text))));
                this.TaoGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuanJieGu_N80.Text))));
                this.ChengPinJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.ChengPinJieGu_N80.Text))));
            }
        }

        protected void Select_SelectedIndexChanged(object sender, EventArgs e)
        {         
            if (this.Select.SelectedValue == "年度")
            {                
                this.JiDu0.Visible = false;
             
                this.YueFen.Visible = false;
                this.YueFen.Items.Clear();
                this.YueFen.Items.Add(new ListItem("年度", "0"));
                
            }
            if (this.Select.SelectedValue == "季度")
            {
                
                this.JiDu0.Visible = true;
             
                this.YueFen.Visible = true;
                this.YueFen.Items.Clear();
                this.YueFen.Items.Add(new ListItem("1季度","13"));
                this.YueFen.Items.Add(new ListItem("2季度", "14"));
                this.YueFen.Items.Add(new ListItem("3季度", "15"));
                this.YueFen.Items.Add(new ListItem("4季度", "16"));
            }
            if (this.Select.SelectedValue == "月份")
            {
              
                this.JiDu0.Visible = false;
           
                this.YueFen.Visible = true;
                this.YueFen.Items.Clear();
                this.YueFen.Items.Add(new ListItem("1月份", "1"));
                this.YueFen.Items.Add(new ListItem("2月份", "2"));
                this.YueFen.Items.Add(new ListItem("3月份", "3"));
                this.YueFen.Items.Add(new ListItem("4月份", "4"));
                this.YueFen.Items.Add(new ListItem("5月份", "5"));
                this.YueFen.Items.Add(new ListItem("6月份", "6"));
                this.YueFen.Items.Add(new ListItem("7月份", "7"));
                this.YueFen.Items.Add(new ListItem("8月份", "8"));
                this.YueFen.Items.Add(new ListItem("9月份", "9"));
                this.YueFen.Items.Add(new ListItem("10月份", "10"));
                this.YueFen.Items.Add(new ListItem("11月份", "11"));
                this.YueFen.Items.Add(new ListItem("12月份", "12"));
            }
        }      
        protected void Button_Export_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            HSSFSheet sheet1 = hssfworkbook.GetSheet("原料采购计划");
            string title="";
            if (this.Select.SelectedValue == "年度")
            {
                title = this.NianDu.SelectedItem.Text + "年";
            }
            if (this.Select.SelectedValue == "季度")
            {
                title = this.NianDu.SelectedItem.Text + "年"+this.YueFen.SelectedItem.Text;               
            }
            if (this.Select.SelectedValue == "月份")
            {
                title = this.NianDu.SelectedItem.Text + "年" + this.YueFen.SelectedItem.Text;  
            }

            sheet1.GetRow(0).GetCell(0).SetCellValue(title + "原料采购计划");
            string 时间 = DateTime.Now.ToString("yyyy-MM-dd");
            sheet1.GetRow(1).GetCell(2).SetCellValue("时间:" + 时间);

            sheet1.GetRow(3).GetCell(5).SetCellValue(YouGuan.Text);
            sheet1.GetRow(3).GetCell(7).SetCellValue(YouGuanJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(3).GetCell(9).SetCellValue(YouGuanJiHuaZhiJinJine.Text);


            sheet1.GetRow(4).GetCell(5).SetCellValue(TaoGuan.Text);
            sheet1.GetRow(4).GetCell(7).SetCellValue(TaoGuanJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(4).GetCell(9).SetCellValue(TaoGuanJiHuaZhiJinJine.Text);

            sheet1.GetRow(5).GetCell(5).SetCellValue(YouGuanJieGu.Text);
            sheet1.GetRow(5).GetCell(7).SetCellValue(YouGuanJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(5).GetCell(9).SetCellValue(YouGuanJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(6).GetCell(5).SetCellValue(TaoGuanJieGu.Text);
            sheet1.GetRow(6).GetCell(7).SetCellValue(TaoGuanJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(6).GetCell(9).SetCellValue(TaoGuanJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(7).GetCell(5).SetCellValue(ChengPinJieGu.Text);
            sheet1.GetRow(7).GetCell(7).SetCellValue(ChengPinJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(7).GetCell(9).SetCellValue(ChengPinJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(8).GetCell(5).SetCellValue(YouGuan_N80.Text);
            sheet1.GetRow(8).GetCell(7).SetCellValue(YouGuanJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(8).GetCell(9).SetCellValue(YouGuanJiHuaZhiJinJine_N80.Text);

            sheet1.GetRow(9).GetCell(5).SetCellValue(TaoGuan_N80.Text);
            sheet1.GetRow(9).GetCell(7).SetCellValue(TaoGuanJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(9).GetCell(9).SetCellValue(TaoGuanJiHuaZhiJinJine_N80.Text);           

            sheet1.GetRow(10).GetCell(5).SetCellValue(YouGuanJieGu_N80.Text);
            sheet1.GetRow(10).GetCell(7).SetCellValue(YouGuanJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(10).GetCell(9).SetCellValue(YouGuanJieGuJiHuaZhiJinJine_N80.Text);    

            sheet1.GetRow(11).GetCell(5).SetCellValue(TaoGuanJieGu_N80.Text);
            sheet1.GetRow(11).GetCell(7).SetCellValue(TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(11).GetCell(9).SetCellValue(TaoGuanJieGuJiHuaZhiJinJine_N80.Text);           

            sheet1.GetRow(12).GetCell(5).SetCellValue(ChengPinJieGu_N80.Text);
            sheet1.GetRow(12).GetCell(7).SetCellValue(ChengPinJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(12).GetCell(9).SetCellValue(ChengPinJieGuJiHuaZhiJinJine_N80.Text);       


            WriteToFile();
            Response.End();
        }
       
    }
}
